import api from '../../../api/api'
import {updateUser } from "../../../api/conf";
import Toast from 'tdesign-miniprogram/toast/index';

Page({
  data: {
    nameValue: '',
  },
  onLoad(options) {
    const { name } = options;
    this.setData({
      nameValue: name,
    });
  },
  onSubmit() {
    if(!this.data.nameValue){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '昵称不能为空',
      });
      return;
    }
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.showNavigationBarLoading()
    api.post(updateUser, {
      model:{'Nickname':this.data.nameValue}
    }).then(data => {
      wx.hideNavigationBarLoading()
      wx.hideLoading()
      let pages = getCurrentPages();
      let prevPage = pages[pages.length -2]
      prevPage.setData({
        nickName: this.data.nameValue
      })
      wx.setStorageSync('nickname', this.data.nameValue)
      wx.navigateBack({ backRefresh: true });
    })
  },
  clearContent() {
    this.setData({
      nameValue: '',
    });
  },
  onBlur: function (e) {
    this.setData({
      nameValue: e.detail.value,
    });
  }
});
